package com.boot.hospital.score.core.controller;

import com.boot.hospital.score.common.enums.ResultCodeEnum;
import com.boot.hospital.score.common.wrapper.ResponseWrapper;
import com.boot.hospital.score.core.service.ScoreService;
import com.boot.hospital.score.core.service.UserService;
import com.boot.hospital.score.dao.entity.CadreAndScore;
import com.boot.hospital.score.dao.entity.Review;
import com.boot.hospital.score.dao.entity.Score;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评分业务
 */
@CrossOrigin(origins = "*")
@Slf4j

@PropertySources(value = {@PropertySource(value = {"classpath:important.properties", "classpath:application.properties"},
        encoding = "utf-8")})
@RestController
@RequestMapping("/score")
@EnableAutoConfiguration
public class ScoreController {

    @Resource
    private ScoreService scoreService;
//    @ApiOperation("查询all评分记录")
//    @GetMapping(value = "/selectAll")
//    public ResponseWrapper<Object> selectAll() {
//        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
//
//        List<Score> scoreList = scoreService.selectAll();
//        responseWrapper.setData(scoreList);
//
//        return responseWrapper;
//    }

    @ApiOperation("增加评分")
    @PostMapping(value = "/addScores")
    public ResponseWrapper<Object> addScore(@RequestBody Score score) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            if (score.getExpertId().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("专家id为空");
                return responseWrapper;
            }
            if (score.getCadresId().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员id为空");
                return responseWrapper;
            }
            int result = scoreService.insert(score);
            if (result > 0) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.FAIL.getName());
            }
        } catch (Exception e) {
            log.error("增加评分失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }

    @ApiOperation("查询score和cadre")
    @GetMapping(value = "/getScoresAndCadres")
    public ResponseWrapper<Object> getScoresAndCadres(@RequestParam Integer reviewId, @RequestParam Integer expertId) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("expertId", expertId);
            params.put("reviewId", reviewId);

            CadreAndScore scoresAndCadres = scoreService.getScoresAndCadres(params);

            responseWrapper.setData(scoresAndCadres);
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
            responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
        } catch (Exception e) {
            log.error("增加评分失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }

    @ApiOperation("检查当前专家的评审是否已经结束")
    @GetMapping(value = "/checkExpertOccur")
    public ResponseWrapper<Object> checkExpertOccur(@RequestParam Integer reviewId, @RequestParam Integer expertId) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("expertId", expertId);
            params.put("reviewId", reviewId);

            Score score = scoreService.selectByReviewIdAndExpertId(params);

            if (score != null) {
                responseWrapper.setCode("500");
                responseWrapper.setMsg("当前评审已结束");
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
                responseWrapper.setMsg("评审未结束");
            }

        } catch (Exception e) {
            log.error("检查当前专家的评审是否已经结束失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }
}
